#include "DoubleLinkList.h"
#include <iostream>

DoubleLinkList::DoubleLinkList():size(0)
{
    head = tail= new Node(0);
}

DoubleLinkList::~DoubleLinkList()
{
    while(head != nullptr)
    {
        Node *nextNode = head->next;// 下一个节点存下载
        delete head;head = nextNode;
        tail = nullptr;
    }
}

void DoubleLinkList::InsertTail(const ElementType &element)
{
    Node *newNode = new Node(element);
    tail->next = newNode;
    newNode->prev = tail;
    tail = newNode;
    size++;
}


void DoubleLinkList::InsertByIndex(size_t index, const ElementType &element)
{
    if(index >= size)
    {
        std::cout<<"位置非法!\n";
        return;
    }
    Node *travelPoint = head;
    for(size_t i=0;i < index;i++)
    {
        travelPoint =travelPoint->next;
    }
    Node *newNode = new Node(element);
    newNode->next=travelPoint->next;
    newNode->prev =travelPoint;
    travelPoint->next =newNode;
    newNode->next->prev =newNode;
    size++;
}



void DoubleLinkList::show()
{
    std::cout<<"size : "<<size<<std::endl;
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)//链表还没有结束
    {
        std::cout<<travelPoint->value<<" ";
        travelPoint = travelPoint->next;//遍历指针往后走
    }
    std::cout<<std::endl;

    travelPoint = tail;
    while(travelPoint != head)
    {
        std::cout<< travelPoint->value << " ";
        travelPoint =travelPoint->prev;
    }
    std::cout<<std::endl;

}